# constants.py
import pygame

# 游戏窗口配置
WIDTH, HEIGHT = 800, 600
BLOCK_SIZE = 20
SPEED = 5

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 贪吃蛇")

# 时钟控制帧率
clock = pygame.time.Clock()

# 字体设置,默认的arial显示不了中文，需要使用指定的微软雅黑字体
try:
    font = pygame.font.Font("resource/Microsoft Yahei.ttf", 24)
except:
    font = pygame.font.SysFont("arial", 24)


# 定义精灵图中的各个部分的像素值，40像素
SPRITE_SIZE = BLOCK_SIZE * 2
try:
    spritesheet = pygame.image.load("resource/snake.png").convert_alpha()

    # 蛇头方向 (右, 左, 上, 下)
    head_right = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(4 * SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    head_left = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(4 * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    head_up = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(3 * SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    head_down = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(3 * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))

    # 蛇尾方向 (右, 左, 上, 下)
    tail_right = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(0, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    tail_left = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    tail_up = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    tail_down = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(0, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))

    # 蛇身弯角 (左上, 右上, 左下, 右下)
    corner_tl = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(6 * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 左上
    corner_tr = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(5 * SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 右上
    corner_bl = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(6 * SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 左下
    corner_br = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(5 * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 右下

    # 蛇身直段 (水平, 垂直)
    straight_h = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(2 * SPRITE_SIZE, 0, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 水平
    straight_v = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(2 * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))  # 垂直

    # 创建精灵字典
    snake_sprites = {
        "head": {
            "RIGHT": head_right,
            "LEFT": head_left,
            "UP": head_up,
            "DOWN": head_down,
        },
        "tail": {
            "RIGHT": tail_right,
            "LEFT": tail_left,
            "UP": tail_up,
            "DOWN": tail_down,
        },
        "corner": {
            "TL": corner_tl,  # 左上
            "TR": corner_tr,  # 右上
            "BL": corner_bl,  # 左下
            "BR": corner_br,  # 右下
        },
        "straight": {"H": straight_h, "V": straight_v},  # 水平  # 垂直
    }
    print("蛇精灵图加载成功")
except Exception as e:
    print(f"蛇精灵图加载失败: {str(e)}")
    snake_sprites = None

try:
    spritesheet = pygame.image.load("resource/food.png").convert_alpha()
    FOOD_BLOCK_SIZE = 16
    # 蛇头方向 (右, 左, 上, 下)
    apple = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(0, 0, FOOD_BLOCK_SIZE, FOOD_BLOCK_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    drinks = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(FOOD_BLOCK_SIZE, 0, FOOD_BLOCK_SIZE, FOOD_BLOCK_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    pizza = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(2 * FOOD_BLOCK_SIZE, 0, FOOD_BLOCK_SIZE, FOOD_BLOCK_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    ice_cream = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(3 * FOOD_BLOCK_SIZE, 0, FOOD_BLOCK_SIZE, FOOD_BLOCK_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    bread = pygame.transform.scale(spritesheet.subsurface(pygame.Rect(4 * FOOD_BLOCK_SIZE, 0, FOOD_BLOCK_SIZE, FOOD_BLOCK_SIZE)), (BLOCK_SIZE, BLOCK_SIZE))
    # 创建精灵字典
    food_sprites = {"apple": apple, "drinks": drinks, "pizza": pizza, "ice_cream": ice_cream, "bread": bread}
    print("食物精灵图加载成功")
except Exception as e:
    print(f"食物精灵图加载失败: {str(e)}")
    food_sprites = None


# 测试显示精灵图
def test_display_snake_sprites():
    """测试显示所有加载的精灵图"""
    print("测试显示食物精灵图")

    # 创建测试窗口
    test_screen = pygame.display.set_mode((1600, 1600))
    pygame.display.set_caption("精灵图测试")

    # 定义显示位置
    x, y = 50, 50
    spacing = SPRITE_SIZE + 10

    # 显示头部精灵
    if snake_sprites:
        # 显示蛇头，文字部分高30像素
        title = font.render("蛇头精灵", True, WHITE)
        test_screen.blit(title, (x, y - 30))
        test_screen.blit(snake_sprites["head"]["RIGHT"], (x, y))
        test_screen.blit(snake_sprites["head"]["LEFT"], (x + spacing, y))
        test_screen.blit(snake_sprites["head"]["UP"], (x + 2 * spacing, y))
        test_screen.blit(snake_sprites["head"]["DOWN"], (x + 3 * spacing, y))

        # 显示尾部精灵
        title = font.render("蛇尾精灵", True, WHITE)
        test_screen.blit(title, (x, y + SPRITE_SIZE))
        test_screen.blit(snake_sprites["tail"]["RIGHT"], (x, y + SPRITE_SIZE + 30))
        test_screen.blit(snake_sprites["tail"]["LEFT"], (x + spacing, y + SPRITE_SIZE + 30))
        test_screen.blit(snake_sprites["tail"]["UP"], (x + 2 * spacing, y + SPRITE_SIZE + 30))
        test_screen.blit(snake_sprites["tail"]["DOWN"], (x + 3 * spacing, y + SPRITE_SIZE + 30))

        # 显示角落精灵
        title = font.render("角落精灵", True, WHITE)
        test_screen.blit(title, (x, y + 2 * SPRITE_SIZE + 30))
        test_screen.blit(snake_sprites["corner"]["TL"], (x, y + 2 * SPRITE_SIZE + 60))  # 左上
        test_screen.blit(snake_sprites["corner"]["TR"], (x + spacing, y + 2 * SPRITE_SIZE + 60))  # 右上
        test_screen.blit(snake_sprites["corner"]["BL"], (x + 2 * spacing, y + 2 * SPRITE_SIZE + 60))  # 左下
        test_screen.blit(snake_sprites["corner"]["BR"], (x + 3 * spacing, y + 2 * SPRITE_SIZE + 60))  # 右下

        # 显示直段精灵
        title = font.render("直段精灵", True, WHITE)
        test_screen.blit(title, (x, y + 3 * SPRITE_SIZE + 60))
        test_screen.blit(snake_sprites["straight"]["H"], (x, y + 3 * SPRITE_SIZE + 90))  # 水平
        test_screen.blit(snake_sprites["straight"]["V"], (x + spacing, y + 3 * SPRITE_SIZE + 90))  # 垂直

        # 添加说明文本
        instructions = [
            "蛇头: RIGHT, LEFT, UP, DOWN",
            "蛇尾: RIGHT, LEFT, UP, DOWN",
            "角落: TL(左上), TR(右上), BL(左下), BR(右下)",
            "直段: H(水平), V(垂直)",
        ]

        for i, text in enumerate(instructions):
            text_surface = font.render(text, True, WHITE)
            test_screen.blit(text_surface, (50, y + 3 * SPRITE_SIZE + 130 + i * 30))

        pygame.display.update()

        # 等待按键退出
        waiting = True
        while waiting:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                    waiting = False

    # 恢复游戏窗口
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Pygame 贪吃蛇")


# 测试显示精灵图
def test_display_food_sprites():
    """测试显示所有加载的精灵图"""
    print("测试显示食物精灵图")
    try:
        # 创建测试窗口
        test_screen = pygame.display.set_mode((1600, 1600))
        pygame.display.set_caption("精灵图测试")

        # 定义显示位置
        x, y = 50, 50
        spacing = SPRITE_SIZE + 10

        if food_sprites:
            # 显示蛇头，文字部分高30像素
            title = font.render("食物精灵", True, WHITE)
            test_screen.blit(title, (x, y - 30))
            test_screen.blit(food_sprites[apple], (x, y))
            test_screen.blit(food_sprites[drinks], (x + spacing, y))
            test_screen.blit(food_sprites[pizza], (x + 2 * spacing, y))
            test_screen.blit(food_sprites[ice_cream], (x + 3 * spacing, y))
            test_screen.blit(food_sprites[bread], (x + 4 * spacing, y))

            # 添加说明文本
            text_surface = font.render("苹果，饮料，披萨，冰淇淋，面板", True, WHITE)
            test_screen.blit(text_surface, (x, SPRITE_SIZE + 100))

            pygame.display.update()

            # 等待按键退出
            waiting = True
            while waiting:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                        waiting = False

        # 恢复游戏窗口
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("Pygame 贪吃蛇")

    except Exception as e:
        print(e)
        return
